package com.aisino.mplearning;

import com.aisino.mplearning.utils.SqlMetaUtils;

import java.sql.*;

/**
 * @author guochuantao
 * @version 1.0
 * @description
 * @since 2024/9/6 下午4:58
 */
public class ColumnTypeCheckerPostgres {
    public static void main(String[] args) {
        String url = "jdbc:postgresql://172.24.103.45:5432/postgres"; // 替换为你的数据库URL
        String user = "postgres"; // 替换为你的数据库用户名
        String password = "Sjzl@2024"; // 替换为你的数据库密码
        String tableName = "xxl_job_log"; // 替换为你想要查询的表名

        try {
            //加载驱动
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        try (Connection conn = DriverManager.getConnection(url, user, password)) {
            DatabaseMetaData metaData = conn.getMetaData();
            ResultSet columns = metaData.getColumns(null, "public", tableName, null);

            ResultSet rs = conn.createStatement().executeQuery("SELECT \"table_name\",\"column_name\",is_nullable," +
                    "data_type,character_maximum_length,character_octet_length," +
                    "numeric_precision,numeric_scale,datetime_precision \n" +
                    "-- SELECT * \n" +
                    "FROM information_schema.columns where table_schema = 'public' and \"table_name\" = \n" +
                    "'xxl_job_log'");
            System.out.println("columns = " + columns);
            while (columns.next() && rs.next()) {
                String columnName = columns.getString("COLUMN_NAME");
                int columnType = columns.getInt("DATA_TYPE");
                int decimalDigits = columns.getInt("DECIMAL_DIGITS");
                int columnSize = columns.getInt("COLUMN_SIZE");
                // 输出列名和类型
                System.out.println("Column Name: " + columnName + ", =====>SQL Type: " + columnType + "====>精度：" + decimalDigits + "     columnSize: " + columnSize);

                String name = rs.getString("COLUMN_NAME");
                System.out.print("name = " + name + "\t");
                String type = rs.getString("data_type");
                System.out.print("data_type = " + type + "\t");
                String type1 = rs.getString("character_maximum_length");
                System.out.print("character_maximum_length = " + type1 + "\t");
                int numericScare = rs.getInt("numeric_scale");
                System.out.print("numeric_scale = " + numericScare + "\t");
                int numericPrecision = rs.getInt("numeric_precision");
                System.out.print("numericPrecision = " + numericPrecision + "\t");
                System.out.println("====" + SqlMetaUtils.getDbLengthForPostgres(rs));
                int datetimePrecision = rs.getInt("datetime_precision");
                System.out.println("datetimePrecision = " + datetimePrecision + "\t");
                System.out.println();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}